/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "EditBox.h"
#include <xml/Stream.h>

namespace liba
{
namespace lib2d
{
namespace gui
{

EditBox::EditBox( Object * parent )
:	Object( parent, Rect( 0, 300, 100, 50 ) ),
	cursor_pos( 0 ),
	sel_end( 0 )
{
	view = new view::EditBox( this );
	AutoPtr< resource::Resource > res = resource::Manager::create_resource( "editbox.xml" );
	xml::StreamResource( res ).load( &xml::Root( view, "EditBoxData" ) );
}

EditBox::~EditBox()
{}

bool EditBox::on_attribute( xml::Provider * prov, const std::string & name, const std::string & value )
{
	return false;
}

bool EditBox::on_node( xml::Provider * prov, const std::string & name )
{
	return false;
}

void EditBox::on_text( const std::wstring & text )
{
	if( !iscntrl( text[0] ) )
	{
		if( cursor_pos != sel_end )
		{
			std::pair< int, int > sel = get_selection_range();
			cursor_pos = sel.first;
			this->text.erase( sel.first, sel.second - sel.first );
		}

		this->text.insert( cursor_pos, text );
		++cursor_pos;
		 sel_end = cursor_pos;
	}
	//TODO action
}

void EditBox::on_key_down( const std::string & key_name )
{
	if( key_name == "Key#35" )//End
		sel_end = cursor_pos = text.size();
	if( key_name == "Key#36" )//Home
		sel_end = cursor_pos = 0;

	if( key_name == "BackSpace" )
	{
		if( cursor_pos != sel_end )
		{
			std::pair< int, int > sel = get_selection_range();
			cursor_pos = sel.first;
			text.erase( sel.first, sel.second - sel.first );
		}
		else
		if( text.size() && cursor_pos )
		{
			--cursor_pos;
			text.erase( cursor_pos, 1 );
			//TODO action
		}
		sel_end = cursor_pos;
	}
	if( key_name == "Delete" )
	{
		if( cursor_pos != sel_end )
		{
			std::pair< int, int > sel = get_selection_range();
			cursor_pos = sel.first;
			text.erase( sel.first, sel.second - sel.first );
		}
		else
		if( cursor_pos < text.size() )
		{
			text.erase( cursor_pos, 1 );
			//TODO action
		}
		sel_end = cursor_pos;
	}
	if( key_name == "Left" )
	{
		if( cursor_pos > 0 )
			--cursor_pos;

		sel_end = cursor_pos;
	}
	if( key_name == "Right" )
	{
		if( cursor_pos < text.size() )
			++cursor_pos;

		sel_end = cursor_pos;
	}
}

void EditBox::on_button_down( const Vector2 & point, const Atom & button_name )
{
	cursor_pos = view->get_selected_index( point );
	sel_end = cursor_pos;
	Object::on_button_down( point, button_name );
}

void EditBox::on_button_up( const Vector2 & point, const Atom & button_name )
{
	Object::on_button_up( point, button_name );
}

void EditBox::on_mouse_move( const Vector2 & point )
{
	if( !mouse_pressed )
		return;
	cursor_pos = view->get_selected_index( point );
}

void EditBox::on_mouse_move_outside( const Vector2 & point )
{
	EditBox::on_mouse_move( point );
}

void EditBox::on_focus( bool focus )
{
	if( focus == false )
		sel_end = cursor_pos;

	Object::on_focus( focus );
}

} //namespace gui
} //namespace lib2d
} //namespace liba

